package com.xiao.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.xiao.enums.AppHttpCodeEnum;
import java.io.Serializable;

// <T> 是一个泛型占位符，意味着这个类可以用于封装任意类型的数据。
//统一响应格式。实体类，或者这个类严格来说叫响应体
@JsonInclude(JsonInclude.Include.NON_NULL)// 当序列化（转换成 JSON）对象时，应该忽略掉值为 null 的属性
public class ResponseResult<T> implements Serializable { // 实现序列化
    private Integer code;
    private String msg;
    private T data;

    public ResponseResult() {
        this.code=AppHttpCodeEnum.SUCCESS.getCode();
        this.msg=AppHttpCodeEnum.SUCCESS.getMsg();
    }

    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ResponseResult errorResult(int code,String msg){
        ResponseResult result = new ResponseResult();
        return result.error(code,msg);
    }
    public static ResponseResult okResult(){
        ResponseResult result = new ResponseResult();
        return result;
    }
    public static ResponseResult okResult(int code,String msg){
        ResponseResult result = new ResponseResult();
        return result.ok(code,null,msg);
    }



    public static ResponseResult okResult(Object data){
        // 还是调用 okResult 的方法，把 响应状态码 和 响应信息  set 成一个 ResponseResult
        ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getMsg());
        if (data!=null){// 如果数据不为空 ，也 set 进去;
            result.setData(data);
        }
        return result;// 结果一定是 成功 200 和 成功的响应信息 数据要看结果
    }

    public  static ResponseResult errorResult(AppHttpCodeEnum enums){
        return  setAppHttpCodeEnum(enums,enums.getMsg());
    }

    public  static ResponseResult errorResult(AppHttpCodeEnum enums,String msg){
        return  setAppHttpCodeEnum(enums,msg);
    }
    public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums){
        return  okResult(enums.getCode(),enums.getMsg());
    }
    public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums,String msg){
        return  okResult(enums.getCode(),msg);
    }

    public ResponseResult<?> error(Integer code,String msg){
        this.code=code;
        this.msg=msg;
        return this;
    }
    public ResponseResult<?> ok(Integer code,T data){
        this.code=code;
        this.data=data;
        return this;
    }
    public ResponseResult<?> ok(Integer code,T data,String msg){
        this.code=code;
        this.data=data;
        this.msg=msg;
        return this;
    }

    public ResponseResult<?> ok(T data){
        this.data=data;
        return this;
    }

    public Integer getCode(){
        return  code;
    }
    public Integer setCode(Integer code){
        return  this.code=code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }
}
